from flask_restful import Resource,reqparse,request
from comment.models.index import *
from comment.utils.data2dict import datalist2dict,data2dict
from comment.utils.shopping_redis import redis_client
import json
#商品分类的资源类
class Shopping_Category(Resource):
    def get(self):
        #丛redis中读取数据
        data_cache=redis_client.get('index_category')
        if data_cache:#如果redis中有数据，则返回redis中的数据
            return json.loads(data_cache)
        else:#如果没有数据，丛MySQL中查询数据返回给用户并写入redis
            rq=reqparse.RequestParser()
            rq.add_argument('parent_id',required=True,type=int)
            req=rq.parse_args()
            data=self.getData(req.parent_id)
            if data:
                return data
            for item in data:
                item.update({'list':''})
                data_second =self.getData(item["id"])
                item['list'] =data_second
                for item1 in data_second:
                    item1.update({'item1':''})
                    data_third=self.getData(item1['id'])
                    item1['list']=data_third
                #把查询到的数据写入redis中
                redis_client.setes('index_category',3600*24,json.dumps(data))
                return data
            else:
                return {'message':'none'}


    @staticmethod
    def getData(parent_id):
        # 从数据库中取数据
        res = Ctegory.query.with_entities(Ctegory.id, Ctegory.parent_id, Ctegory.name).filter(
            Ctegory.parent_id == parent_id).order_by(Ctegory.sort.asc()).all()
        # 判断
        if res:
            return datalist2dict(res)
        else:
            return {'message': 'None'}

class  Dict_test(Resource):
#取多条数据所有字段
    def get(self):
        res=Ctegory.query.filter(Ctegory.parent_id==0).all()
        if res:
            return datalist2dict(res)
        else:
            return 'none'
#首页新品推荐的资源类
class Shopping_Home_NewProduct(Resource):
    def get(self):
        res=HomeNewProduct.query.join(Product,HomeNewProduct.product_id==Product.id).with_entities(Product.id,Product.price,Product.default_pic,Product.product_name,
                                                                                                   Product.rel_category3_id).order_by(HomeNewProduct.sort.asc())\
            .limit(10).all()
        if res:
            return datalist2dict(res)
        else:
            return {'message':'none'}
#人气热搜资源类
class HomeRecommendProduct(Resource):
    def get(self):
        data_catch=redis_client.get('index_HomeRecommendProduct')
        if data_catch:
            return json.loads(data_catch)
        else:
            res=HomeRecommendProduct.query.join(Product,HomeRecommendProduct.product_id==Product.id)\
            .with_entities(Product.id,Product.default_pic,Product.product_name,Product.rel_category3_id,Product.price)\
            .limit(10).all()
            if res:
                data=datalist2dict(res)
                #将查询数据写入redis数据库中
                redis_client.setes('index_HomeRecommendProduct',3600*24,json.dumps(data))
                return data
            else:
                return {'message':'None'}
#首页专题资源类,专题分类表有问题
class Shopping_RecommendSubject(Resource):
    def get(self):
        data_catch=redis_client.get('index_RecommendSubject')
        if data_catch:
            return json.loads(data_catch)
        else:
            res=CmsSubjecct.query.filter(CmsSubjecct.show_status==1).all()
            if res:
                data=datalist2dict(res)
                for i in range(len(data)):
                    res_product=CmsSubjectProductRelation.query.json(CmsSubjectProductRelation.product_id==Product.id)\
                    .filter(CmsSubjectProductRelation.subject_id==data[i]['id']).with_entities\
                        (Product.id,Product.default_pic,Product.price,Product.product_name,Product.rel_category3_id)\
                        .limit(10).all()
                    if res_product:
                        data [i]['productList']=datalist2dict(res_product)
                    else:
                        data[i]['productList']=''
                #将数据写入redis数据库中
                redis_client.setes('index_RecommendSubject',2600*24,json.dumps(data))
            else:
                return {'message':'None'}
